<template>
  <div>
    <!-- 大客户部部长审批 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>
    <!--  -->
    <FilterTable
      ref="table"
      columnsRef="qjhBigCustomerMinisterApproval"
      :columns="columns"
      :isSelectColumn="false"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :cellstyles="cellstyles"
      :onSyncTableData="syncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <Drawer :visible.sync="visible" :title="drawerTitle" :width="'80%'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :type="handleType" :rowData="formData" @close="emitcloseHandler" />
    </Drawer>

    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList } from '@/api/omFinance/FinanceManage/BigCustomerMinisterApproval';

import SonModel from './SonModel';
import OwnerSearch from '../OwnerSearch';

export default {
  name: 'BigCustomerMinisterApproval',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '审核', show: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),

      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      visible: false,
      isDealer: false,
      handleType: '',
      drawerTitle: '',
      dealerCodes: '',
      cellstyles: []
    };
  },
  components: {
    SonModel,
    OwnerSearch
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    syncTableData(data) {
      if (!data.length) return;
      data.forEach(x => {
        if (x.isRiskDealer == 10041001) {
          this.cellstyles.push({ row: x, dataIndex: 'isRiskDealer', styles: { backgroundColor: 'red' } });
        }
        if (x.overdueQuantity > 0) {
          this.cellstyles.push({ row: x, dataIndex: 'overdueQuantity', styles: { backgroundColor: 'red' } });
        }
        if (x.stockFactor > x.stockFactorLimit) {
          this.cellstyles.push({ row: x, dataIndex: 'stockFactor', styles: { backgroundColor: 'red' } });
        }
      });
    },
    searchHandle() {
      this.isDealer = true;
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitcloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.dealerCodes = dealerCodes.toString();
        this.topFilterList.find(x => x.fieldName == 'dealerCodes').initialValue = dealerCodes.toString();
      }
    },
    changeHandle(params) {
      this.fetchParams.dealerCodes = this.dealerCodes;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    resetChange(params) {
      this.dealerCodes = '';
      this.fetchParams.dealerCodes = '';
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCodes',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.searchHandle()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '银行名称',
          fieldName: 'bankName',
          placeholder: '请选择'
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'applyDateBegin|applyDateEnd',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '申请类型',
          fieldName: 'applyType',
          placeholder: '',
          initialValue: 60731002,
          disabled: true,
          itemList: this.createDictList('6073')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" disabled={props.row.isRiskDealer != 10041002} onClick={() => this.editHandle(props.row)}>
                  审核
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'businessStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [{ text: '待事业部部长审批', value: 60301003 }],
          render: props => {
            return <span>{this.createDictText(props.row.businessStatus, '6030')}</span>;
          }
        },
        {
          title: '申请单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '银行名称',
          dataIndex: 'bankName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '申请类型',
          dataIndex: 'applyType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [{ text: '大客户融资', value: 60731002 }],
          render: props => {
            return <span>{this.createDictText(props.row.applyType, '6073')}</span>;
          }
        },
        {
          title: '出票金额（万）',
          dataIndex: 'billAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '出票期限',
          dataIndex: 'billDeadline', // TODO 字典6027
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6027'),
          render: props => {
            return <span>{this.createDictText(props.row.billDeadline, '6027')}</span>;
          }
        },
        {
          title: '银行联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '银行联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '申请日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-rangge'
        },
        {
          title: '提车公司',
          dataIndex: 'salesCompany', // TODO 字典2555
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2555'),
          render: props => {
            return <span>{this.createDictText(props.row.salesCompany, '2555')}</span>;
          }
        },
        {
          title: '收款方',
          dataIndex: 'payee',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6031'),
          render: props => {
            return <span>{this.createDictText(props.row.payee, '6031')}</span>;
          }
        },
        {
          title: '本月已开票金额',
          dataIndex: 'invoiceAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '上月终端',
          dataIndex: 'lastSalesQuantity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '前三月平均终端',
          dataIndex: 'averageSalesQuantity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库存',
          dataIndex: 'stockQuantity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库存系数',
          dataIndex: 'stockFactor',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库存系数上限',
          dataIndex: 'stockFactorLimit',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '目前库销比',
          dataIndex: 'stockSalesRatio',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '超期车台数',
          dataIndex: 'overdueQuantity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否为风险关注店',
          dataIndex: 'isRiskDealer', // TODO 1004
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isRiskDealer, '1004')}</span>;
          }
        },
        {
          title: '移动比例',
          dataIndex: 'moveRatio',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '回款率',
          dataIndex: 'collectionRate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'applyRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
